package com.key.controller.admin.systemSetting;

import com.key.common.R;
import com.key.common.vo.DepartmentAndCompanyVO;
import com.key.entity.Company;
import com.key.entity.Department;
import com.key.service.OrganizationalStructureService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author：朕
 * @Package：com.key.controller.admin.systemSetting
 * @Project：ams_system
 * @name：OrganizationalStructure
 * @Date：2023/2/18 14:37
 * @Filename：OrganizationalStructure
 */
@Controller
@Api(value = "组织架构",tags = "组织架构的API")
@RequestMapping(value = "/employee")
public class OrganizationalStructureController {

    @Autowired
    private OrganizationalStructureService organizationalStructureService;



    /**
     * 公司
     * @return
     */
    @RequestMapping(value = "/findCompany",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "显示公司信息",notes = "",httpMethod = "get",response = R.class)
    public R findCompany(){
        R r = new R();
        List<Company> companies = organizationalStructureService.findCompany();
        if(companies.size()>0){
            r.setCode(200);
            r.setMsg("显示成功");
            r.setData(companies);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("显示失败");
            return r;
        }
    }


    @RequestMapping(value = "/addCompany",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加公司",notes = "传递公司实体类信息",httpMethod = "post", response = R.class)
    public R addSysRole(@RequestBody @ApiParam(name = "Company",value = "公司的实体类",required = true) Company company){
        R r = new R();
        boolean o = organizationalStructureService.addCompany(company);
        if(o==true){
            r.setCode(200);
            r.setMsg("增加成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("增加失败");
            return r;
        }
    }


    @RequestMapping(value = "/deleteCompany",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除公司",notes = "传递公司编码",httpMethod = "post",response = R.class)
    public R deleteCompany(@RequestBody @ApiParam(name = "companyId",value = "公司Id",required = true) Integer companyId) {
        R r = new R();
        boolean o = organizationalStructureService.deleteCompany(companyId);
        if(o==true){
            r.setCode(200);
            r.setMsg("删除成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("删除失败");
            return r;
        }
    }


    @RequestMapping(value = "/findCompanyById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询公司",notes = "传递公司编码",httpMethod = "get",response = R.class)
    public R findCompanyById(@RequestBody @ApiParam(name = "companyId",value = "公司编码",required = true)Integer companyId ){
        R r = new R();
        List<Company> companies = organizationalStructureService.findCompanyById(companyId);
        if(companies.size()>0){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(companies);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("查询失败");
            return r;
        }
    }


    @RequestMapping(value = "/updateCompany",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改公司",notes = "传递修改的参数",httpMethod = "post",response = R.class)
    public R updateCompany(@RequestBody @ApiParam(name = "Company",value = "公司的修改信息",required = true) Company company){
        R r = new R();
        boolean o = organizationalStructureService.updateCompany(company);
        if(o==true){
            r.setCode(200);
            r.setMsg("修改成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("修改失败");
            return r;
        }
    }


    /**
     * 部门
     * @return
     */
    @RequestMapping(value = "/findDepartment",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "显示部门信息",notes = "",httpMethod = "get",response = R.class)
    public R findDepartment(){
        R r = new R();
        List<Department> departments = organizationalStructureService.findDepartment();
        if(departments.size()>0){
            r.setCode(200);
            r.setMsg("部门显示成功");
            r.setData(departments);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("部门显示失败");
            return r;
        }
    }


    @RequestMapping(value = "/addDepartment",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加部门",notes = "传递部门实体类信息",httpMethod = "post", response = R.class)
    public R addDepartment(@RequestBody @ApiParam(name = "Department",value = "部门的实体类",required = true) Department department){
        R r = new R();
        boolean o = organizationalStructureService.addDepartment(department);
        if(o==true){
            r.setCode(200);
            r.setMsg("增加成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("增加失败");
            return r;
        }
    }


    @RequestMapping(value = "/deleteDepartment",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除部门",notes = "传递部门编码",httpMethod = "get",response = R.class)
    public R deleteDepartment(@RequestBody @ApiParam(name = "departmentId",value = "部门Id",required = true) Integer departmentId) {
        R r = new R();
        boolean o = organizationalStructureService.deleteDepartment(departmentId);
        if(o==true){
            r.setCode(200);
            r.setMsg("删除成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("删除失败");
            return r;
        }
    }


    @RequestMapping(value = "/findDepartmentById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据部门ID查询部门",notes = "传递部门编码",httpMethod = "get",response = R.class)
    public R findDepartmentById(@RequestBody @ApiParam(name = "departmentId",value = "部门编码",required = true)Integer departmentId ){
        R r = new R();
        List<Department> departments = organizationalStructureService.findDepartmentById(departmentId);
        if(departments.size()>0){
            r.setCode(200);
            r.setMsg("部门查询成功");
            r.setData(departments);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("部门查询失败");
            return r;
        }
    }


    @RequestMapping(value = "/updateDepartment",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改部门",notes = "传递修改的参数",httpMethod = "get",response = R.class)
    public R updateDepartment(@RequestBody @ApiParam(name = "Department",value = "部门的修改信息",required = true) Department department){
        R r = new R();
        boolean o = organizationalStructureService.updateDepartment(department);
        if(o==true){
            r.setCode(200);
            r.setMsg("修改成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("修改失败");
            return r;
        }
    }


    /**
     * 显示公司和对应的部门
     * @return
     */
    @RequestMapping(value = "/findCompanyAndDepartment",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "显示公司和对应的部门",notes = "",httpMethod = "get",response = R.class)
    public R findCompanyAndDepartment(){
        R r = new R();
        List<DepartmentAndCompanyVO> dac = organizationalStructureService.findCompanyAndDepartment();
        System.out.println(1);
        if(dac.size()>0){
            r.setCode(200);
            r.setMsg("显示成功");
            r.setData(dac);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("显示失败");
            return r;
        }
    }


}
